(function() {
  var _container = document.getElementById('gr');
  var _players = [new deberc.gameRoomViewPlayer('gr_0'), new deberc.gameRoomViewPlayer('gr_1')];

  document.getElementById('gr_l').onclick = function() {
    woody.dom.hide(_container);
    deberc.gameRoom.leave();
  };

  document.getElementById('gr_k_k').onclick = function() {
    woody.dom.hide(document.getElementById('gr_k'));
    deberc.gameRoom.move('k');
  };

  document.getElementById('gr_k_p').onclick = function() {
    woody.dom.hide(document.getElementById('gr_k'));
    deberc.gameRoom.move('p');
  };

  document.getElementById('gr_t_S').onclick = function() {
    woody.dom.hide(document.getElementById('gr_t'));
    deberc.gameRoom.move('t;S');
  };

  document.getElementById('gr_t_C').onclick = function() {
    woody.dom.hide(document.getElementById('gr_t'));
    deberc.gameRoom.move('t;C');
  };

  document.getElementById('gr_t_H').onclick = function() {
    woody.dom.hide(document.getElementById('gr_t'));
    deberc.gameRoom.move('t;H');
  };

  document.getElementById('gr_t_D').onclick = function() {
    woody.dom.hide(document.getElementById('gr_t'));
    deberc.gameRoom.move('t;D');
  };

  document.getElementById('gr_t_p').onclick = function() {
    woody.dom.hide(document.getElementById('gr_t'));
    deberc.gameRoom.move('w');
  };

  deberc.gameRoomView = {
    hide: function() {
      woody.dom.hide(_container);
    },
    wait: function() {
      var waitNode = document.getElementById('gr_w');
      if (deberc.gameRoom.isMyIndex(deberc.gameRoom.getWhichTurn())) {
        waitNode
          .innerHTML = '<div class="d bgray">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ваш ход ...</div>';
      }
      else {
        waitNode.innerHTML = '<div class="d bgray">Ожидайте соперника ...</div>';
        vk.api.getUserInfo(deberc.gameRoom.getPlayers()[deberc.gameRoom.getWhichTurn()], this, function(info) {
          waitNode.innerHTML = '<div class="d bgray">Ожидайте соперника <a href="' + info.url + '" class="link">' +
            info.name + '</a> ...</div>';
        })
      }

      woody.dom.show(waitNode);
    },
    stopWaiting: function() {
      woody.dom.hide(document.getElementById('gr_w'));
    },
    render: function() {
      woody.dom.hide(document.getElementById('gr_h'));
      woody.dom.hide(document.getElementById('gr_g'));
      woody.dom.hide(document.getElementById('gr_u'));
      woody.dom.hide(document.getElementById('gr_o'));
      woody.dom.hide(document.getElementById('gr_k'));
      woody.dom.hide(document.getElementById('gr_t'));

      switch (deberc.gameRoom.getPhase()) {
        case '1':

          woody.dom.show(_container);
          for (var i in deberc.gameRoom.getPlayers()) {
            _players[i].init(deberc.gameRoom.getPlayers()[i], i == deberc.gameRoom.getDealer(), 0,
              deberc.gameRoom.isMyIndex(i)
                ? deberc.gameRoom.getCards()
                : 6);
          }

          var deck = document.getElementById('gr_u');
          woody.dom.show(deck);
          deck.childNodes[deck.childNodes.length - 1].className = 'c C' + deberc.gameRoom.getUpCard();

          var obyaz = document.getElementById('gr_o');
          woody.dom.show(obyaz);
          obyaz = obyaz.childNodes[obyaz.childNodes.length - 1];
          if (deberc.gameRoom.isMyIndex(deberc.gameRoom.getDealer())) {
            obyaz.innerHTML = 'Вас';
          }
          else {
            obyaz.innerHTML = 'Сопернике';
          }
          vk.api.getUserInfo(deberc.gameRoom.getPlayers()[deberc.gameRoom.getDealer()], this, function(info) {
            obyaz.innerHTML = info.name;
            obyaz.className = 'c link';
          })

          document.getElementById('gr_k_m').className = "m C" + deberc.gameRoom.getUpCard().substr(1);
          if (deberc.gameRoom.isMyIndex(deberc.gameRoom.getWhichTurn())) {
            this.stopWaiting();
            woody.dom.show(document.getElementById('gr_k'));
          }
          else {
            this.wait();
          }
          break;
        case '2':

          woody.dom.show(_container);
          for (var i in deberc.gameRoom.getPlayers()) {
            _players[i].init(deberc.gameRoom.getPlayers()[i], i == deberc.gameRoom.getDealer(), 0,
              deberc.gameRoom.isMyIndex(i)
                ? deberc.gameRoom.getCards()
                : 6);
          }

          var deck = document.getElementById('gr_u');
          woody.dom.show(deck);
          deck.childNodes[deck.childNodes.length - 1].className = 'c C' + deberc.gameRoom.getUpCard();

          var obyaz = document.getElementById('gr_o');
          woody.dom.show(obyaz);
          obyaz = obyaz.childNodes[obyaz.childNodes.length - 1];
          if (deberc.gameRoom.isMyIndex(deberc.gameRoom.getDealer())) {
            obyaz.innerHTML = 'Вас';
          }
          else {
            obyaz.innerHTML = 'Сопернике';
          }
          vk.api.getUserInfo(deberc.gameRoom.getPlayers()[deberc.gameRoom.getDealer()], this, function(info) {
            obyaz.innerHTML = info.name;
            obyaz.className = 'c link';
          })

          if (deberc.gameRoom.isMyIndex(deberc.gameRoom.getWhichTurn())) {
            this.stopWaiting();
            var koz = document.getElementById('gr_t');
            woody.dom.show(koz);
            koz.className = "status " + deberc.gameRoom.getUpCard().substr(1);
            if (deberc.gameRoom.isMyIndex(deberc.gameRoom.getDealer())) {
              woody.dom.hide(document.getElementById('gr_t_p'));
            }
            else {
              woody.dom.show(document.getElementById('gr_t_p'));
            }
          }
          else {
            this.wait();
          }
          break;
        case '3':

          woody.dom.show(_container);
          for (var i in deberc.gameRoom.getPlayers()) {
            _players[i].init(deberc.gameRoom.getPlayers()[i], i == deberc.gameRoom.getDealer(), 0,
              deberc.gameRoom.isMyIndex(i)
                ? deberc.gameRoom.getCards()
                : 9);
          }

          var deck = document.getElementById('gr_u');
          woody.dom.show(deck);
          deck.childNodes[deck.childNodes.length - 1].className = 'c C' + deberc.gameRoom.getUpCard();

          var igraet = document.getElementById('gr_g');
          igraet = igraet.childNodes[igraet.childNodes.length - 1];
          if (deberc.gameRoom.isMyIndex(deberc.gameRoom.getWhichTurn())) {
            igraet.innerHTML = 'Вы';
          }
          else {
            igraet.innerHTML = 'Соперник';
          }
          woody.dom.show(document.getElementById('gr_g'));

          vk.api.getUserInfo(deberc.gameRoom.getPlayers()[deberc.gameRoom.getWhichTurn()], this, function(info) {
            igraet.innerHTML = info.name;
            igraet.className = 'c link';
          })

          this.wait();

          woody.dom.show(document.getElementById('gr_h'));
          document.getElementById('gr_h_k').className = "C" + deberc.gameRoom.getKozir();

          break;
        //        case 'p':
        //          _whichTurn = (_whichTurn + 1) % _players.length;
        //          if (deberc.gameRoom.isMyIndex(_whichTurn)) {
        //            this.stopWaiting();
        //            woody.dom.show(document.getElementById('gr_k'));
        //            document.getElementById('gr_k_m').className = "m C" + data[2].substr(1);
        //          }
        //          else {
        //            woody.dom.hide(document.getElementById('gr_k'));
        //            this.wait();
        //          }
        //          break;
        //        case 'k':
        //          woody.dom.hide(document.getElementById('gr_o'));
        //          woody.dom.hide(document.getElementById('gr_k'));
        //
        //          var igraet = document.getElementById('gr_g');
        //          igraet = igraet.childNodes[igraet.childNodes.length - 1];
        //          if (deberc.gameRoom.isMyIndex(_whichTurn)) {
        //            igraet.innerHTML = 'Вы';
        //          }
        //          else {
        //            igraet.innerHTML = 'Соперник';
        //          }
        //          woody.dom.show(document.getElementById('gr_g'));
        //
        //          vk.api.getUserInfo(deberc.gameRoom.getPlayers()[_whichTurn], this, function(info) {
        //            igraet.innerHTML = info.name;
        //            igraet.className = 'c link';
        //          })
        //
        //          this.wait();
        //
        //          woody.dom.show(document.getElementById('gr_h'));
        //          document.getElementById('gr_h_k').className = "C" + _upCard.substr(1);
        //
        //          break;
      }
    }
  };
})();
